/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.client;

import info.client.InfoClient;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
/**
 *
 * @author mark
 */
public class ManagementInfo extends JFrame implements ActionListener {   
    
    //Menubar
    private JMenuBar menubar = new JMenuBar();
    //Menu-tabs
    private JMenu file;
    //Menu-items
    private JMenuItem exit;
    
    private String query;
    private String[] kolommen;

    private JPanel panel = new JPanel();

    private JButton get_managementinfo;
    private JComboBox dropdown;

    private DefaultTableModel dtm;
    private JScrollPane scrollpane;
    private JTabbedPane tabs;
    
    private String rapport;
    private String tabel;
    
    public ManagementInfo() {
        panel.setLayout(null);
        
        file = new JMenu("Bestand");

        exit = new JMenuItem("Exit");
        
        this.setResizable(false);
        
        menubar.add(file);
        
        file.add(exit);
        
        exit.addActionListener(this);
        
        //Tabs
        tabs = new JTabbedPane();
        tabs.setBounds(0, 70, 800, 500);
        
        //Buttons
        get_managementinfo = new JButton("Ophalen");
        get_managementinfo.setBounds(230, 30, 100, 30);
        get_managementinfo.addActionListener(this);
        
        //Dropdown menu's
        dropdown = new JComboBox();
        dropdown.setBounds(10, 30, 200, 20);
        
        menubar.setBounds(0, 0, 800, 20);
        
        String reportquery = "SELECT rapportnaam From rapporten";
        InfoClient.socketconnection.send(reportquery);
        
        Object[][] rowresult = InfoClient.socketconnection.read();
                
        for(Object[] ro : rowresult)
        {
            for(Object oa : ro)
            {
                dropdown.addItem(oa);
            }
        }
        
        //Content toevoegen aan het paneel
        panel.add(dropdown);
        panel.add(get_managementinfo);
        panel.add(menubar);
        
        setTitle("Info Client");
        setContentPane(panel);
        setSize(800, 600);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void fillTable() {
        
        String str = rapport;
        String str2 = null;
        String str3 = null;
        String _kolom;

        String[] temp = null;
        String[] temp2;
        String[] temp3;
        String[] temp4 = null;
 
          /* delimiter */
          String delimiter = "FROM ";
          String delimiter5 = "SELECT ";
          String delimiter6 = "= ";
          String delimiter2 = ";";
          String delimiter3 = ",";
          String delimiter4 = " ";
            /* given string will be split by the argument delimiter provided. */

            temp = str.split(delimiter);
            //de tabel wordt eruitgehaald, maar er kan een ; achter staan
            str2 = temp[1];
            temp2 = str2.split(delimiter2);
            //de tabel heeft nu geen ; erachter waardoor hij op de juiste manier wordt doorgestuurd
            String _tabel = temp2[0].replace("[","").replace("]", "");
            tabel = _tabel;
            temp3 = _tabel.split(delimiter4);
            
            String splitter = ";";
            String temp5[];
            String temp6[];
            
            if(temp3.length > 1)
            {
                for(int i = 0; i < temp3.length; i++)
                {
                    if("WHERE".equals(temp3[i].toUpperCase()))
                    {
                        splitter = temp3[i];
                    }
                    else if("JOIN".equals(temp3[i].toUpperCase()))
                    {
                        splitter = temp3[i];
                    }
                    else if("GROUP".equals(temp3[i].toUpperCase()))
                    {
                        splitter = temp3[i];
                        
                    }
                }
                
                temp5 = temp[1].split(splitter);
                //Select * FROM [gegevens]
                temp6 = temp5[0].split(delimiter);
                //[gegevens]
                _tabel = temp6[0].replace("[","").replace("]", "");
                //gegevens
                tabel = _tabel;
            }

            //Select kolom1, kolom2, kolom3 etc.
            str3 = temp[0]; 
            
            //kolom1, kolom2, kolom3 etc.
            temp = str3.split(delimiter5);
            
            //[0] = kolom1, [1] = kolom2 etc.
            temp = temp[1].split(delimiter3);
            
            
            temp4 = str.split(delimiter6);

            String _rapport = "";
            
            
            // /mark/ > 'mark'
            if(temp4.length > 1)
            {
                for(int i = 0; i < temp4.length; i++)
                {
                    String _result = temp4[i].replace("/", "'");
                    if(i == 0)
                    {
                        temp4[i] = _result;
                    }
                    else
                    {
                        temp4[i] = "= " + _result;
                    }
                    _rapport += temp4[i];
                }
            }
            
            if(!_rapport.equals(""))
            {
                rapport = _rapport;
            }
            
            int _kolommen = 0;
            
            for(int i = 0; i < temp.length; i++)
            {

                _kolommen++;

            }
            
            String[] klm = new String[_kolommen];
            int _klm = 0;
            int extrakolom = 0;
            boolean containsCount = false;
            
            for(int i = 0; i < temp.length; i++)
            {
                if(temp[i].contains("COUNT"))
                {
                    klm[_klm] = "COUNT";
                    extrakolom++;
                    _klm++;
                    containsCount = true;
                }
                else
                {
                    String _result = temp[i].replaceAll(",", "");
                    String result = _result.replace("[","").replace("]", "");
                    klm[_klm] = result;
                    _klm++;
                }
            }
 
            if(klm[0].equals("* "))
            {
                _kolom = " AND COLUMN_NAME = '*'";
            }
            else
            {            
                _kolom = " AND";
            
                for(int i = 0; i < klm.length; i++)
                {
                    if(i == 0)
                    {
                         _kolom = _kolom + " COLUMN_NAME = '" + klm[i] + "'";
                    }
                    else
                    {
                        _kolom = _kolom + " OR COLUMN_NAME = '" + klm[i] + "'";
                    }
                }
            }

        if(_kolom.equals(" AND COLUMN_NAME = '*'"))
        {
            //haalt alle kolommen uit de gewenste tabel
            query = "SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" + tabel + "'";
        }
        else
        {
            if(containsCount == true)
            {
                //haalt specifieke kolommen uit de gewenste tabel
                query = "SELECT DISTINCT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" + tabel + "'" + _kolom;
            }
            else
            {
                //haalt specifieke kolommen uit de gewenste tabel
                query = "SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" + tabel + "'" + _kolom;
            }
        }

        //Het verzenden van de inlogquery over de socket
        InfoClient.socketconnection.send(query);
        
        Object[][] uitkomst = InfoClient.socketconnection.read();
        
        //kijkt hoeveel kolommen eruit de query komen, zodat we weten hoe groot de array moet worden
        int j = 0;
        for(Object[] k : uitkomst)
        {
            for(Object kk : k)
            {
                j++;         
            }
        }
        
        //de kolommen voor de jtable
        kolommen = new String[j + extrakolom];
        
        int i = 0;
        for(Object[] k : uitkomst)
        {
            for(Object kk : k)
            {
                if(_kolom.equals(" AND COLUMN_NAME = '*'"))
                {
                    kolommen[i] = kk.toString();
                }
                else
                {
                    kolommen[i] = temp[i];
                }
                i++;
            }
        }
        
        //Het verzenden van de inlogquery over de socket
        InfoClient.socketconnection.send(rapport);
        //Het ophalen van de resultaten van de query
        Object[][] uitkomst2 = InfoClient.socketconnection.read();
        dtm = new DefaultTableModel(uitkomst2, kolommen);
        
        //maakt een nieuwe jtable aan met de genoemde kolommen en de uitkomst van de query
        JTable table = new JTable(dtm);
        
        JTableHeader header = table.getTableHeader();
        header.setBounds(0, 70, 800, 20);
        
        table.setPreferredScrollableViewportSize(new Dimension(800, 200));
        table.setFillsViewportHeight(true);
        
        scrollpane = new JScrollPane(table);

        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_ALWAYS);
        
        if(tabs.getTabCount() > 0)
        {
            tabs.remove(0);
        }
        //Voeg de tabel toe aan de tabs
        tabs.add(rapport, scrollpane);
        table.setEnabled(false);
        panel.add(tabs);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        //Als er op exit is geklikt in het menu
        if(e.getSource() == exit)
        {
            this.setVisible(false);
        }
        //Als er op de get_managementinfo knop is geklikt
        if(e.getSource() == get_managementinfo)
        {
            //Kijk welk Query is geselecteerd om op te halen.
            String selecteditem = dropdown.getSelectedItem().toString();
            //Het verzenden van de inlogquery over de socket
            InfoClient.socketconnection.send("SELECT rapport from rapporten where rapportnaam ='" + selecteditem + "'");
            //Het get_managementinfo van de resultaten van de query
            Object[][] result3 = InfoClient.socketconnection.read();
            for(Object[] ro : result3)
            {
                for(Object oa : ro)
                {
                     rapport = oa.toString();
                }
            }
            //Vul de table    
            fillTable();
        }
    }

}
